package io.github.shang11925.sbvuewelearn.controller;

import io.github.shang11925.sbvuewelearn.dao.CoursesRepository;
import io.github.shang11925.sbvuewelearn.dto.CoursesDTO;
import io.github.shang11925.sbvuewelearn.entity.Courses;
import io.github.shang11925.sbvuewelearn.response.CoursesResponse;
import io.github.shang11925.sbvuewelearn.service.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:5173/")
public class CoursesController {
    @Autowired
    CoursesService coursesService;
    @Autowired
    CoursesRepository coursesRepository;

    @GetMapping("/list-courses")
    public CoursesResponse list(){
        List<Courses> courses = coursesRepository.findAll();
        CoursesResponse coursesResponse = new CoursesResponse();
        coursesResponse.setCourses(courses);
        return coursesResponse;
    }

    @PostMapping("add-courses")
    public void add(@RequestBody CoursesDTO coursesDTO){
        coursesService.add(coursesDTO);
    }
    @DeleteMapping("/delete-courses/{id}")
    public void delete(@PathVariable Long id){
        coursesRepository.deleteById(id);
    }
    @PutMapping("/update-courses/{id}")
    public void update(@PathVariable Long id, @RequestBody CoursesDTO coursesDTO) {
        coursesService.update(id, coursesDTO);
    }
}